﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BenhVien
{
    public partial class ThemThuocVaoKho : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        Connect conn = new Connect();
        public ThemThuocVaoKho()
        {
            InitializeComponent();
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMaThuoc.Text == "" | txtTenThuoc.Text == "" | txtMT.Text == "" | txtLL.Text == "" | txtCD.Text == "")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                if (check("Select MaThuoc from Thuoc where MaThuoc =@DoiSo", txtMaThuoc.Text) == true)
                    MessageBox.Show("Ma bi trung hãy cập nhật", "Loi", MessageBoxButtons.OK);
                else
                {
                    DataRow row = dt.NewRow();
                    row["MaThuoc"] = txtMaThuoc.Text;
                    row["TenThuoc"] = txtTenThuoc.Text;
                    row["MoTa"] = txtMT.Text;
                    row["SoLuong"] = txtLL.Text;
                    row["CachDung"] = txtCD.Text;
                    dt.Rows.Add(row);
                    SqlCommand commandInsert = new SqlCommand();
                    commandInsert.Connection = conn.con;
                    commandInsert.CommandType = CommandType.Text;
                    commandInsert.CommandText = @"Insert Thuoc (MaThuoc, TenThuoc, MoTa, SoLuong,CachDung) Values 
                                                        (@MaThuoc, @TenThuoc, @MoTa, @SoLuong, @CachDung)";
                    commandInsert.Parameters.Add("@MaThuoc", SqlDbType.NVarChar, 50, "MaThuoc");
                    commandInsert.Parameters.Add("@TenThuoc", SqlDbType.NVarChar, 50, "TenThuoc");
                    commandInsert.Parameters.Add("@MoTa", SqlDbType.NVarChar, 150, "MoTa");
                    commandInsert.Parameters.Add("@SoLuong", SqlDbType.Int, 50, "SoLuong");
                    commandInsert.Parameters.Add("@CachDung", SqlDbType.NVarChar, 50, "CachDung");
                    da.InsertCommand = commandInsert;
                    da.Update(dt);
                    MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                }
            }
        }
        private bool check(String s, String s1)
        {
            bool check1 = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check1 = true;
            return check1;
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from Thuoc";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;

        }
        private void ThemThuocVaoKho_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
            binding();
        }
        public void binding()
        {
            txtMaThuoc.DataBindings.Clear();
            txtMaThuoc.DataBindings.Add("Text", dataGridView1.DataSource, "MaThuoc");
            txtTenThuoc.DataBindings.Clear();
            txtTenThuoc.DataBindings.Add("Text", dataGridView1.DataSource, "TenThuoc");
            txtMT.DataBindings.Clear();
            txtMT.DataBindings.Add("Text", dataGridView1.DataSource, "MoTa");
            txtLL.DataBindings.Clear();
            txtLL.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuong");
            txtCD.DataBindings.Clear();
            txtCD.DataBindings.Add("Text", dataGridView1.DataSource, "CachDung");
        }
        private void bt_luu_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaThuoc like '" + txtMaThuoc.Text + "'")[0];
            row.BeginEdit();
            row["TenThuoc"] = txtTenThuoc.Text;
            row["MoTa"] = txtMT.Text;
            row["SoLuong"] = txtLL.Text;
            row["CachDung"] = txtCD.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update Thuoc Set TenThuoc= @TenThuoc,
                                                              MoTa= @MoTa,
                                                              SoLuong= @SoLuong,
                                                              CachDung=@CachDung
                                                              where MaThuoc=@MaThuoc";
            commandUpdate.Parameters.Add("@MaThuoc", SqlDbType.NVarChar, 50, "MaThuoc");
            commandUpdate.Parameters.Add("@TenThuoc", SqlDbType.NVarChar, 50, "TenThuoc");
            commandUpdate.Parameters.Add("@MoTa", SqlDbType.NVarChar, 150, "MoTa");
            commandUpdate.Parameters.Add("@SoLuong", SqlDbType.Int, 50, "SoLuong");
            commandUpdate.Parameters.Add("@CachDung", SqlDbType.NVarChar, 50, "CachDung");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        }
    }
}

